using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Resources;
using System.Reflection;
using System.IO;
using Rdpk;
using Urovo;

namespace RR3036.WinCE
{
    public partial class RR3036MainFrm : Form
    {
        private byte readerAddr = 0xff;
        private bool fAppClose = false;
        private int fCmdRet = 0x30;
        private const byte OK = 0x00;
        private const byte lengthError = 0x01;
        private const byte operationNotSupport = 0x02;
        private const byte dataRangError = 0x03;
        private const byte cmdNotOperation = 0x04;
        private const byte RfClosed = 0x05;
        private const byte EEPROM = 0x06;
        private const byte timeOut = 0x0a;
        private const byte moreUID = 0x0b;
        private const byte ISOError = 0x0c;
        private const byte noElectronicTag = 0x0e;
        private const byte operationError = 0x0f;
        private const byte cmdNotSupport = 0x01;
        private const byte cmdNotIdentify = 0x02;
        private const byte unknownError = 0x0f;
        private const byte blockError = 0x10;
        private const byte blockLockedAndCntLock = 0x11;
        private const byte blockLockedAndCntWrite = 0x12;
        private const byte blockCntOperate = 0x13;
        private const byte blockCntLock = 0x14;
        private const byte communicationErr = 0x30;
        private const byte retCRCErr = 0x31;
        private const byte retDataErr = 0x32;
        private const byte communicationBusy = 0x33;
        private const byte executeCmdBusy = 0x34;
        private const byte comPortOpened = 0x35;
        private const byte comPortClose = 0x36;
        private const byte invalidHandle = 0x37;
        private const byte invalidPort = 0x38;
        private bool isStop15693Inventory = true;
        private bool isStop14443BGetTag = true;
        private byte[] DSFIDAndUID = new byte[160];
        private byte[] selectUID = new byte[8];
        private byte ISO15693TagBlockSize = 0;
        private byte[] ultraLightUID = new byte[7];
        private byte[] mifareUID = new byte[4];

        public RR3036MainFrm()
        {
            InitializeComponent();
            btnConnect.Enabled = true;
            btnDisconnect.Enabled = false;
        }
        private string RR3036GetErrorCodeDesc(byte errorCode)
        {
            switch (errorCode)
            {
                case cmdNotSupport:
                    return "Commands not supported";
                case cmdNotIdentify:
                    return "Unknown Command";
                case unknownError:
                    return "Unknown error";
                case blockError:
                    return "Appointed  block can't be used or does not exist";
                case blockLockedAndCntLock:
                case blockLockedAndCntWrite:
                    return "Appointed  block is locked";
                case blockCntOperate:
                    return "Appointed  block can't be operated normally";
                case blockCntLock:
                    return "Appointed  block can't be locked normally";
                case cmdNotOperation:
                    return "CMD can't execute at current";
                case 0x33:
                    return "Utralight tag anticollision failed";
                case 0x32:
                    return "MF1 and Utralight tag collision";
                case 0x31:
                    return "Unallow more than one tag in RF field";
                case 0x30:
                    return "Anticollision failed";
                case 0x2E:
                    return "Utralight tag write failed";
                case 0x2D:
                    return "Value operation failed";
                case 0x2C:
                    return "CheckWrite data not match";
                case 0x2B:
                    return "Check write failed";
                case 0x2A:
                    return "Storage key failed";
                case 0x29:
                    return "Read/Write E2PROM failed";
                case 0x28:
                    return "Transfer failed";
                case 0x27:
                    return "Increment/Decrement failed";
                case 0x26:
                    return "Read e-Wallet failed";
                case 0x25:
                    return "e-Wallet initialization failed";
                case 0x24:
                    return "Write failed";
                case 0x23:
                    return "Read failed";
                case 0x22:
                    return "Authentication failed";
                case 0x21:
                case 0x35:
                    return "Select failed";
                case 0x20:
                    return "No ISO14443A tag";
                case 0x34:
                    return "No ISO14443B tag";
                case 0x36:
                    return "Halt failed";
                case 0x37:
                    return "Transparent command failed";
                case 0x38:
                    return "Anticollision failed";
                default:
                    return "";
            }
        }

        private string RR3036GetReturnCodeDesc(int retCode)
        {
            switch (retCode)
            {
                case OK: return "success";
                case lengthError: return "Command operand length error";
                case operationNotSupport: return "Command not supported";
                case RfClosed: return "RF field is inactive";
                case EEPROM: return "E2PROM operation error";
                case timeOut: return "InventoryScanTime overflow,no UID collected";
                case moreUID: return "Not all tag's UID are collected before the InventoryScanTime overflow";
                case ISOError: return "ISO error";
                case noElectronicTag: return "No Tags";
                case operationError: return "Operation error";
                case communicationErr: return "Communication Error";
                case retCRCErr: return "CRC checksummat Error";
                case retDataErr: return "Return data error";
                case communicationBusy: return "Communication busy";
                case cmdNotOperation: return "CMD can't execute at current";
                case dataRangError: return "Operation data range error";
                case invalidHandle: return "Invalid PortHandle";
                case 0x1F: return "Work pattern error";
                default:
                    return  "";
            }
        }

       private void ShowResultDialog(string cmdStr, int cmdRet, byte errorCode)
        {
            string s = "";
            try
            {
                if (!fAppClose)
                {
                    if (cmdRet == 0x10)
                    {
                        if (errorCode == 0)
                            s = cmdStr + ": \r\n" + RR3036GetReturnCodeDesc(cmdRet);
                        else
                            s = cmdStr + ": \r\n" + RR3036GetErrorCodeDesc(errorCode);
                    }
                    else
                    {
                        s = cmdStr + ": " + RR3036GetReturnCodeDesc(cmdRet) + " " + RR3036GetErrorCodeDesc(errorCode);
                    }
                    MessageBox.Show(s, "Information");
                }
            }
            finally
            {
                ;
            }
        }

        private void ShowResultDialog(string cmdStr, int cmdRet)
        {
            string tp;
            try
            {
                tp = RR3036GetReturnCodeDesc(cmdRet);
                if (tp != "")
                    MessageBox.Show(cmdStr + ": " + tp,"Information");
                else
                    MessageBox.Show(cmdStr + ": \r\n return " + "0x" + cmdRet.ToString("X2"), "Information"); ;
            }
            finally
            {
                ;
            }
        }

        private void btnConnect_Click(object sender, EventArgs e)
        {
            readerAddr = 0xFF;
            byte[] verion = new byte[2];	    //软件版本
            byte model = 0;				        //读写器型号
            byte[] supProtocol = new byte[2];	//支持的协议
            byte inventoryScanTime = 0;
            int result = 0x30;

            result = RWDev.ConnectReader();
            if (result == OK)
            {
                RWDev.GetReaderInfo(ref readerAddr, verion, ref model, supProtocol, ref inventoryScanTime);
                MessageBox.Show("ConnectReader success.", "Communication");
                btnConnect.Enabled = false;
                btnDisconnect.Enabled = true;
                
            }
            else
            {
                MessageBox.Show("ConnectReader failed.", "Communication");
                btnConnect.Enabled = true;
                btnDisconnect.Enabled = false;
            }
        }

        private void btnOpenRF_Click(object sender, EventArgs e)
        {
            if (btnConnect.Enabled == true)
            {
                MessageBox.Show("Connect to the reader first", "Warning");
                return;
            }
            ShowResultDialog("OpenRF", RWDev.OpenRF(readerAddr));
        }

        private void btnCloseRF_Click(object sender, EventArgs e)
        {
            if (btnConnect.Enabled == true)
            {
                MessageBox.Show("Connect to the reader first", "Warning");
                return;
            }
            ShowResultDialog("CloseRF", RWDev.CloseRF(readerAddr));
        }

        private void btnDisconnect_Click(object sender, EventArgs e)
        {
            if (RWDev.DisconnectReader() == OK)
            {
                MessageBox.Show("DisconnectReader success.", "Communication");
                btnConnect.Enabled = true;
                btnDisconnect.Enabled = false;
            }
            else
            {
                MessageBox.Show("DisconnectReader failed.", "Communication");
                btnConnect.Enabled = false;
                btnDisconnect.Enabled = true;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (btnConnect.Enabled == true)
            {
                MessageBox.Show("Connect to the reader first", "Warning");
                return;
            }
            ShowResultDialog("ChangeTo15693", RWDev.ChangeTo15693(readerAddr));
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (btnConnect.Enabled == true)
            {
                MessageBox.Show("Connect to the reader first", "Warning");
                return;
            }
            ShowResultDialog("ChangeTo14443A", RWDev.ChangeTo14443A(readerAddr));
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (btnConnect.Enabled == true)
            {
                MessageBox.Show("Connect to the reader first", "Warning");
                return;
            }
            ShowResultDialog("ChangeTo14443B", RWDev.ChangeTo14443B(readerAddr));
        }

        private byte[] HexStringToByteArray(string s)
        {
            s = s.Replace(" ", "");
            byte[] buffer = new byte[s.Length / 2];
            for (int i = 0; i < s.Length; i += 2)
                buffer[i / 2] = (byte)Convert.ToByte(s.Substring(i, 2), 16);
            return buffer;
        }

        private string ByteArrayToHexString(byte[] data)
        {
            StringBuilder sb = new StringBuilder(data.Length * 3);
            foreach (byte b in data)
                sb.Append(Convert.ToString(b, 16).PadLeft(2, '0'));
            return sb.ToString().ToUpper();

        }

        private void btn15693StartScan_Click(object sender, EventArgs e)
        {
            if (btnConnect.Enabled == true)
            {
                MessageBox.Show("Connect to the reader first", "Warning");
                return;
            }
            isStop15693Inventory = false;
            timer1.Enabled = true;
            btn15693StartScan.Enabled = false;
            btn15693StopScan.Enabled = true;
        }

        private void btn15693StopScan_Click(object sender, EventArgs e)
        {
            if (btnConnect.Enabled == true)
            {
                MessageBox.Show("Connect to the reader first", "Warning");
                return;
            }
            isStop15693Inventory = true;
            timer1.Enabled = false;
            btn15693StartScan.Enabled = true;
            btn15693StopScan.Enabled = false;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            byte state = 0x06;
            byte i, AFI = 0;
            byte CardNum = 0;
            string uidTemp = "",temp = "";
            byte errorCode = 0;
            byte[] PUPI = new byte[4];
            byte[] AppData = new byte[4];
            byte[] ProtocolInfo = new byte[3];

            timer1.Enabled = false;
            if (tabControl1.SelectedIndex == 1)
            {
                fCmdRet = RWDev.Inventory(readerAddr, state, AFI, DSFIDAndUID, ref CardNum);
                if (fCmdRet != 0x0E)
                {
                    byte[] daw = new byte[9 * CardNum];
                    Array.Copy(DSFIDAndUID, daw, CardNum * 9);
                    uidTemp = ByteArrayToHexString(daw);
                    for (i = 0; i < CardNum; i++)
                    {
                        temp = uidTemp.Substring(18 * i + 2, 16);
                        listBoxUIDList.Items.Add(temp);
                        if (comboBox15693UIDList.Items.IndexOf(temp) == -1)
                            comboBox15693UIDList.Items.Add(temp);
                    }
                }
            }
            else
            {
                fCmdRet = RWDev.ISO14443BRequestNoAFI(readerAddr, 1, PUPI, AppData, ProtocolInfo, ref errorCode);
                if (fCmdRet == 0)
                {
                    listBox14443BUIDList.Items.Add(ByteArrayToHexString(PUPI));   
                }
            }
            if ((!isStop15693Inventory) || (!isStop14443BGetTag))
                timer1.Enabled = true;
        }

        private void btnClean15693UID_Click(object sender, EventArgs e)
        {
            listBoxUIDList.Items.Clear();
        }

        private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
        {
            timer1.Enabled = false;
            isStop15693Inventory = true;
            isStop14443BGetTag = true;
            if ((tabControl1.SelectedIndex == 1) ||
                (tabControl1.SelectedIndex == 2))
            {
                if (btnConnect.Enabled == false)
                    RWDev.ChangeTo15693(readerAddr);
            }
            else if (tabControl1.SelectedIndex == 3)
            {
                if (btnConnect.Enabled == false)
                    RWDev.ChangeTo14443A(readerAddr);
            }
            else if (tabControl1.SelectedIndex == 4)
            {
                if (btnConnect.Enabled == false)
                    RWDev.ChangeTo14443B(readerAddr);
            }
        }

        private void GetTagInfoStr(byte FirmCode, byte OtherCode, ref string FirmName, ref byte BlockSize, ref byte BlockNum, ref string TagType)
        {
            switch (FirmCode)
            {
                case 0x02:
                    BlockSize = 4;
                    BlockNum = 16;
                    TagType = "B";
                    FirmName = "STMicroelectronics";
                    break;
                case 0x04:
                    BlockSize = 4;
                    BlockNum = 28;
                    TagType = "B";
                    FirmName = "Philips Semiconductors";
                    break;
                case 0x05:
                    BlockSize = 4;
                    BlockNum = 58;
                    if (OtherCode == 0xE0)
                        BlockNum = 250;
                    else if (OtherCode == 0x60)
                        BlockNum = 58;
                    TagType = "B";
                    FirmName = "Infineon";
                    break;
                case 0x07:
                    BlockSize = 4;
                    BlockNum = 64;
                    TagType = "A";
                    FirmName = "Texas Instruments";
                    break;
                case 0x08:
                    BlockSize = 8;
                    BlockNum = 250;
                    TagType = "A/B";
                    FirmName = "Fujitsu";
                    break;
                case 0x16:
                    BlockSize = 4;
                    BlockNum = 14;
                    TagType = "A";
                    FirmName = "EM Microelectronic";
                    break;
                default:
                    FirmName = "";
                    break;
            }
        }

        private void comboBox15693UIDList_SelectedIndexChanged(object sender, EventArgs e)
        {
            byte state = 0;
            byte informationFlag = 0;
            byte DSFID = 0;
            byte AFI = 0;
            byte ICReference = 0;
            byte[] UIDO = new byte[8];
            byte[] memorySize = new byte[2];
            byte blockNum = 0;
            string tagWriteType = "";
            string FirmName = "";


            selectUID = HexStringToByteArray(comboBox15693UIDList.SelectedItem.ToString());
            fCmdRet = RWDev.GetTagInfo(readerAddr, state, selectUID, ref informationFlag, UIDO, 
                                       ref DSFID, ref AFI, memorySize, ref ICReference);
            textBox15693MaxBlock.Text = "";
            if (fCmdRet == 0)
            {
                GetTagInfoStr(UIDO[6], UIDO[7], ref FirmName, ref ISO15693TagBlockSize, ref blockNum, ref tagWriteType);
                if (tagWriteType.IndexOf("A") >= 0)
                    comboBox15693TagType.SelectedIndex = 0;
                else
                    comboBox15693TagType.SelectedIndex = 1;
                textBox15693MaxBlock.Text = (blockNum - 1).ToString();
                comboBox15693BlockSize.SelectedIndex = ISO15693TagBlockSize/4 - 1;
            }
            else
            {
                MessageBox.Show("Get tag information failed", "Warning");
                return;
            }
        }

        private void btn15693Read_Click(object sender, EventArgs e)
        {
            byte state = 0;
            byte blockNum = 0;
            byte blockSecStatus = 0;
            textBox15693DataReaded.Text = "";

            if (btnConnect.Enabled == true)
            {
                MessageBox.Show("Connect to the reader first", "Warning");
                return;
            }

            if (comboBox15693UIDList.Items.Count == 0 || comboBox15693UIDList.SelectedItem.ToString() == "")
            {
                MessageBox.Show("Incorrectness UID", "Warning");
                return;
            }
            if (textBox15693BlockNumber.Text != "")             //判断块号
                blockNum = Convert.ToByte(textBox15693BlockNumber.Text.Trim(), 10);
            else
            {
                MessageBox.Show("Incorrectness block number", "Warning");
                return;
            }

            if (ISO15693TagBlockSize == 8)
                state = 4;
            byte[] data = new byte[ISO15693TagBlockSize];

            fCmdRet = RWDev.ReadSingleBlock(readerAddr, state, selectUID, blockNum, ref blockSecStatus, data);
            if (fCmdRet == 0)
                textBox15693DataReaded.Text = blockSecStatus.ToString("X2") + " " + ByteArrayToHexString(data);
        }

        private void btn15693Write_Click(object sender, EventArgs e)
        {
            byte state = 0;
            byte blockNum = 0;

            if (btnConnect.Enabled == true)
            {
                MessageBox.Show("Connect to the reader first", "Warning");
                return;
            }

            if (comboBox15693UIDList.Items.Count == 0 || comboBox15693UIDList.SelectedItem.ToString() == "")
            {
                MessageBox.Show("Incorrectness UID", "Warning");
                return;
            }

            if (textBox15693BlockNumber.Text != "")             //判断块号
                blockNum = Convert.ToByte(textBox15693BlockNumber.Text.Trim(), 10);
            else
            {
                MessageBox.Show("Incorrectness block number", "Warning");
                return;
            }

            byte[] data = new byte[ISO15693TagBlockSize];
            if (textBox15693Data.Text.Trim().Length == ISO15693TagBlockSize * 2)      //要写入的数据是否有效
                data = HexStringToByteArray(textBox15693Data.Text.Trim());
            else
            {
                MessageBox.Show("Data to write error", "Warning");
                return;
            }
            if (ISO15693TagBlockSize == 4)
            {
                if (comboBox15693TagType.SelectedIndex == 0)    
                    state = 0;
                else
                    state = 0x08;
            }
            else
            {
                if (comboBox15693TagType.SelectedIndex == 0)
                    state = 4;
                else
                    state = 0x0C;
            }

            fCmdRet = RWDev.WriteSingleBlock(readerAddr, state, selectUID, blockNum, data);
            ShowResultDialog("WriteSingleBlock", fCmdRet);
        }

        private void btn14443AGetTag_Click(object sender, EventArgs e)
        {
            byte[] data1 = new byte[2];
            byte errorCode = 0;
            byte size = 0;

            if (btnConnect.Enabled == true)
            {
                MessageBox.Show("Connect to the reader first", "Warning");
                return;
            }
            fCmdRet = RWDev.ISO14443ARequest(readerAddr, 1, data1, ref errorCode);      //request all
            if (fCmdRet != 0)
            {
                ShowResultDialog("ISO14443ARequest", fCmdRet,errorCode);
                return;
            }

            textBox14443AUID.Text = "";
            if (ByteArrayToHexString(data1) == "4400") //UltraLight tag
            {
                fCmdRet = RWDev.ISO14443AULAnticoll(readerAddr, ultraLightUID, ref errorCode);
                textBox14443AUID.Text = "";
                if (fCmdRet == 0)
                    textBox14443AUID.Text = ByteArrayToHexString(ultraLightUID);
                else
                {
                    ShowResultDialog("UltraLight Anticoll", fCmdRet,errorCode);
                    return;
                }
            }
            else                               //Mifare 1 tag
            {
                fCmdRet = RWDev.ISO14443AAnticoll(readerAddr, mifareUID, ref errorCode);
                textBox14443AUID.Text = "";
                if (fCmdRet == 0)
                    textBox14443AUID.Text = ByteArrayToHexString(mifareUID);
                else
                {
                    ShowResultDialog("ISO14443AAnticoll", fCmdRet,errorCode);
                    return;
                }

                fCmdRet = RWDev.ISO14443ASelect(readerAddr, mifareUID, ref size, ref errorCode);
                if (fCmdRet != 0)
                {
                    ShowResultDialog("ISO14443ASelect", fCmdRet, errorCode);
                    return;
                }
            }

        }

        private void btn14443ARead_Click(object sender, EventArgs e)
        {
            byte keyStyle = 0, accessSector;
            byte[] key = new byte[6];
            string temp;
            byte block, errorCode = 0;
            byte[] data = new byte[16];

            if (btnConnect.Enabled == true)
            {
                MessageBox.Show("Connect to the reader first", "Warning");
                return;
            }
            if (textBox14443AUID.Text.Length == 8)          //Mifare tag
            {
                if (textBox14443ASector.Text.Trim() != "")
                    accessSector = Convert.ToByte(textBox14443ASector.Text.Trim(), 10);
                else
                {
                    MessageBox.Show("Incorrectness sector", "Warning");
                    return;
                }
                if (textBox14443AKey.Text.Trim().Length == 12)
                    temp = textBox14443AKey.Text.Trim();
                else
                {
                    MessageBox.Show("Incorrectness Key", "Warning");
                    return;
                }
                key = HexStringToByteArray(temp);

                fCmdRet = RWDev.ISO14443AAuthKey(readerAddr, keyStyle, accessSector, key, ref errorCode);
                if (fCmdRet != 0)
                {
                    MessageBox.Show("Authentication failed", "Warning");
                    return;
                }
            }
            else                                       //ultralight tag
                accessSector = 0;
            if (textBox14443ABlockNum.Text.Trim() != "")
            {
                if (accessSector >= 32)
                    block = (byte)(128 + (accessSector - 32) * 16 + Convert.ToByte(textBox14443ABlockNum.Text.Trim(), 10));

                else
                    block = (byte)(accessSector * 4 + Convert.ToByte(textBox14443ABlockNum.Text.Trim(), 10));
            }
            else
            {
                MessageBox.Show("Incorrectness block number", "Warning");
                return;                
            }

            fCmdRet = RWDev.ISO14443ARead(readerAddr, block, data, ref errorCode);
            textBox14443ADataReaded.Text = "";
            if (fCmdRet != 0)
            {
                ShowResultDialog("ISO14443A read", fCmdRet, errorCode);
                return;
            }
            else
                textBox14443ADataReaded.Text = ByteArrayToHexString(data);
        }

        private void btn14443AWrite_Click(object sender, EventArgs e)
        {
            byte keyStyle = 0, accessSector;
            byte[] key = new byte[6];
            string temp;
            byte block, errorCode = 0;
            byte[] data = new byte[16];

            if (btnConnect.Enabled == true)
            {
                MessageBox.Show("Connect to the reader first", "Warning");
                return;
            }
            if (textBox14443AUID.Text.Length == 8)              //mifare 1 tag
            {
                if (rdbtn14443AUseKeyA.Checked)
                    keyStyle = 0;
                else if (rdbtn14443AUseKeyB.Checked)
                    keyStyle = 1;

                if (textBox14443ASector.Text.Trim() != "")
                    accessSector = Convert.ToByte(textBox14443ASector.Text.Trim(), 10);
                else
                {
                    MessageBox.Show("Incorrectness sector", "Warning");
                    return;
                }
                if (textBox14443AKey.Text.Trim().Length == 12)
                    temp = textBox14443AKey.Text.Trim();
                else
                {
                    MessageBox.Show("Incorrectness Key", "Warning");
                    return;
                }
                key = HexStringToByteArray(temp);

                fCmdRet = RWDev.ISO14443AAuthKey(readerAddr, keyStyle, accessSector, key, ref errorCode);
                if (fCmdRet != 0)
                {
                    ShowResultDialog("Authentication", fCmdRet,errorCode);
                    return;
                }
            }
            else                          //ultralight tag
                accessSector = 0;

            if (textBox14443ABlockNum.Text.Trim() != "")
            {
                if (accessSector >= 32)
                    block = (byte)(128 + (accessSector - 32) * 16 + Convert.ToByte(textBox14443ABlockNum.Text.Trim(), 10));

                else
                    block = (byte)(accessSector * 4 + Convert.ToByte(textBox14443ABlockNum.Text.Trim(), 10));
            }
            else
            {
                MessageBox.Show("Incorrectness block number", "Warning");
                return;
            }

            if (textBox14443AWriteData.Text.Trim().Length == textBox14443AWriteData.MaxLength)
                data = HexStringToByteArray(textBox14443AWriteData.Text.Trim());
            else
            {
                MessageBox.Show("Incorrectness data to write", "Warning");
                return;
            }
            fCmdRet = RWDev.ISO14443AWrite(readerAddr, block, data, ref errorCode);
            ShowResultDialog("ISO14443AWrite", fCmdRet, errorCode);
        }

        private void btn14443BStartGetTag_Click(object sender, EventArgs e)
        {
            if (btnConnect.Enabled == true)
            {
                MessageBox.Show("Connect to the reader first", "Warning");
                return;
            }
            isStop14443BGetTag = false;
            timer1.Enabled = true;
            btn14443BStartGetTag.Enabled = false;
            btn14443BStopGetTag.Enabled = true;
        }

        private void btn14443BStopGetTag_Click(object sender, EventArgs e)
        {
            if (btnConnect.Enabled == true)
            {
                MessageBox.Show("Connect to the reader first", "Warning");
                return;
            }
            isStop14443BGetTag = true;
            timer1.Enabled = false;
            btn14443BStartGetTag.Enabled = true;
            btn14443BStopGetTag.Enabled = false;
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            listBox14443BUIDList.Items.Clear();
        }

        private void textBox14443AWriteData_GotFocus(object sender, EventArgs e)
        {
            if (textBox14443AUID.Text.Length == 14)
                textBox14443AWriteData.MaxLength = 8;
            else
                textBox14443AWriteData.MaxLength = 32;
        }

        private void textBox14443AWriteData_KeyPress(object sender, KeyPressEventArgs e)
        {
            label15.Text = "Inputed " + ((textBox14443AWriteData.Text.Length + 1)/2).ToString() + " bytes";
        }

        private void textBox15693Data_GotFocus(object sender, EventArgs e)
        {
            if (ISO15693TagBlockSize == 4)
                textBox15693Data.MaxLength = 8;
            else if (ISO15693TagBlockSize == 8)
                textBox15693Data.MaxLength = 16;
        }

        private void RR3036MainFrm_Load(object sender, EventArgs e)
        {
            Device.BthPowerOff();
            Device.BthPowerOn();
        }

        private void RR3036MainFrm_Closing(object sender, CancelEventArgs e)
        {
            Device.BthPowerOff();
        }       
    }
}